package znbkListRdqh.presenter;

import android.content.Intent;
import android.util.Log;

import znbkListRdqh.api.WeekAscensionListApi;
import znbkListRdqh.entity.WeekAscensionListEntity;
import znbkListRdqh.IWeekAscensionPresenter;

import com.example.lancooznbk.view.base.BaseActivity;
import com.example.lancooznbk.view.base.BaseFragment;
import com.example.lancooznbk.view.base.ILoadMoreDataView;
import znbkListRdqh.fragment.WeekAscensionListFragment;
import znbkTestPaperInfor.ZnbkPaperDef;
import znbkTestPaperRdqh.ZnbkPaperRdqhInfor;
import znbkTestPaperRdqh.activity.WeekAscensionPaperActivity;
import znbkTestPaperRdqh.guide.ReviewPaperGuideActivity;

import com.lancoo.rxretrofitlibrary.http.HttpManager;
import com.lancoo.rxretrofitlibrary.listener.HttpOnNextListener;
import com.lancoo.rxretrofitlibrary.subscribers.ProgressSubscriber;

/**
 * Created by Administrator on 2018\2\2 0002.
 */

public class WeekAscensionListPresenter implements IWeekAscensionPresenter {

    private int lastPosition = 0;
    private ILoadMoreDataView view;
    private int paperState = -1;//记录当前刷新列表对应筛选项
    private int pageIndex = 1;
    private HttpOnNextListener<WeekAscensionListEntity> httpOnNextListener = new HttpOnNextListener<WeekAscensionListEntity>() {
        @Override
        public void onNext(WeekAscensionListEntity weekAscensionListEntity) {


            if (weekAscensionListEntity.getTotalCount() > 0) {
                view.hideLoading();
                if (pageIndex == 1) {
                    view.loadData(weekAscensionListEntity);
                    lastPosition = 0;
                }else {
                    view.loadMoreData(weekAscensionListEntity);
                }

            }else {
                view.showNoData("暂无弱点提升内容");
            }
        }

        @Override
        public void onError(Throwable e) {
            super.onError(e);
            view.showNetError();
        }
    };

    private ProgressSubscriber mSubscriber;

    public WeekAscensionListPresenter(ILoadMoreDataView view) {
        this.view = view;
    }

    private void getData(){
        if (mSubscriber != null && !mSubscriber.isUnsubscribed()){
            mSubscriber.unsubscribe();
        }
        WeekAscensionListApi api = new WeekAscensionListApi(httpOnNextListener, (WeekAscensionListFragment)view);
        api.setPaperForm("2");
        api.setAnswerState(paperState);
        api.setPageIndex(pageIndex);
        api.setPageSize(lastPosition + 10);
        mSubscriber = new ProgressSubscriber(api);
        HttpManager.getInstance().doHttpDeal(api,mSubscriber);
    }

    @Override
    public void getData(boolean isShowLoading) {
        if (isShowLoading) view.showLoading();
        pageIndex = 1;
        getData();
    }

    @Override
    public void getMoreData() {
        pageIndex = pageIndex + 1;
        getData();
    }

    @Override
    public void getDataByStatus(int statusId) {
        if (paperState == statusId) return;
        paperState = statusId;
        getData(false);
    }


    public void openItemDetail(int position, WeekAscensionListEntity.ModelPaperInfoListBean modelPaperInfoListBean) {
        lastPosition = position;
//        //TODO 调用下面方法进行跳转
//        WeekAscensionPaperActivity.launch((BaseFragment) view,modelPaperInfoListBean, ZnbkPaperDef.FROM_WEEK_ASCENSION);
//        Log.e("========PaperID======",""+modelPaperInfoListBean.getPaperID());
        //                ToastUtils.showShort("进入考点列表");
        ZnbkPaperRdqhInfor.ModelPaperInfoListBean = modelPaperInfoListBean;
        ZnbkPaperRdqhInfor.PaperId = modelPaperInfoListBean.getPaperID();
        ReviewPaperGuideActivity.launch((BaseFragment) view,modelPaperInfoListBean,ZnbkPaperDef.FROM_WEEK_ASCENSION_GUIDE);

    }
}